<?
    require_once("mvc/action/ActionForm.php");
    require_once("mvc/action/ActionMessage.php");
    require_once("mvc/action/ActionMessages.php");

    require_once("cms/menu/om/MenuItemI18n.php");


    class BaseItemForm extends ActionForm {

        protected $itemI18n;
        protected $internalLink;
        protected $save;
        protected $module;
        protected $options;
        protected $formImage;
        protected $deleteImage;

        public function __construct() {
            $this->itemI18n = new MenuItemI18n();
            $this->internalLink = null;
            $this->save = null;
            $this->module = null;
            $this->options = null;
            $this->formImage = null;
            $this->deleteImage = false;
        }

        public function getItemI18n() {
            return $this->itemI18n;
        }

        public function setItemI18n(MenuItemI18n $itemI18n) {
            $this->itemI18n = $itemI18n;
        }

        public function getInternalLink() {
            return $this->internalLink;
        }

        public function setInternalLink($internalLink) {
            $this->internalLink = $internalLink;
        }

        public function getSave() {
            return $this->save;
        }

        public function setSave($save) {
            $this->save = $save;
        }

        public function getModule() {
            return $this->module;
        }

        public function setModule($module) {
            $this->module = $module;
        }

        public function getOptions() {
            return $this->options;
        }

        public function setOptions($options) {
            $this->options = $options;
        }

        public function getFormImage() {
            return $this->formImage;
        }

        public function setFormImage($formImage) {
            $this->formImage = $formImage;
        }

        public function getDeleteImage() {
            return $this->deleteImage;
        }

        public function setDeleteImage($deleteImage) {
            $this->deleteImage = $deleteImage;
        }

        public function validate() {

            $errors = new ActionMessages();

            if(is_null($this->getItemI18n()->getMenuItem()->getMenuId()) || strlen($this->getItemI18n()->getMenuItem()->getMenuId()) == 0)
                $errors->add("itemI18n->menuItem->menuId",
                    new ActionMessage("errors.required", true, new ActionMessage("Menu Id", false)));


            if(!is_null($this->getItemI18n()->getMenuItem()->getMenuId()) && strlen($this->getItemI18n()->getMenuItem()->getMenuId()) > 0 && strlen($this->getItemI18n()->getMenuItem()->getMenuId()) > 255)
                $errors->add("itemI18n->menuItem->menuId",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("Menu Id", false),
                        new ActionMessage("255", false))));


            if(is_null($this->getItemI18n()->getName()) || strlen($this->getItemI18n()->getName()) == 0)
                $errors->add("itemI18n->name",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.item.name", true)));


            if(!is_null($this->getItemI18n()->getName()) && strlen($this->getItemI18n()->getName()) > 0 && strlen($this->getItemI18n()->getName()) > 255)
                $errors->add("itemI18n->name",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.item.name", true),
                        new ActionMessage("255", false))));


            if(!is_null($this->getItemI18n()->getMenuItem()->getLink()) && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 0 && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 255)
                $errors->add("itemI18n->menuItem->link",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.item.link", true),
                        new ActionMessage("255", false))));


            if(!is_null($this->getItemI18n()->getMenuItem()->getLink()) && strlen($this->getItemI18n()->getMenuItem()->getLink()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getItemI18n()->getMenuItem()->getLink()))
                $errors->add("itemI18n->menuItem->link",
                    new ActionMessage("errors.url", true, new ActionMessage("prompt.item.link", true)));


            global $ALLOWABLE_IMAGES;
            if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
                $errors->add("formImage",
                    new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

            return $errors;
        }

    }
?>